package logic

import (
	"context"

	"server-go/rpc/reward/internal/models"
	"server-go/rpc/reward/internal/svc"
	"server-go/rpc/reward/types/reward"

	"github.com/zeromicro/go-zero/core/logx"
)

type EmailAccountLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewEmailAccountLogic(ctx context.Context, svcCtx *svc.ServiceContext) *EmailAccountLogic {
	return &EmailAccountLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *EmailAccountLogic) EmailAccount(in *reward.EmailAccountInput) (*reward.EmailAccountOutput, error) {
	out := &reward.EmailAccountOutput{}
	usersDao := &models.UsersDao{Email: in.Email}
	if err := usersDao.First(l.svcCtx.Db); err != nil {
		return out, err
	}
	if usersDao.Id == 0 {
		out.Data = "no email"
		return out, nil
	}
	if usersDao.Password != in.PassWord {
		out.Data = "fail passwd"
		return out, nil
	}
	out.Data = "success"
	out.BaseUserInfo = &reward.Account{
		NickName:  usersDao.Nickname,
		Email:     usersDao.Email,
		Gender:    usersDao.Gender,
		UserIntro: usersDao.Userintro,
		UserName:  usersDao.Username,
		Money:     uint32(usersDao.Money),
	}
	return out, nil
}
